<template>
  <el-form-item label="组织" prop="deptId">
    <el-cascader
      :clearable="false"
      :options="deptSelect"
      v-model="query.deptId"
      collapse-tags
      :props="{ multiple: true, checkStrictly: true, emitPath:false }"
      @change="changeValue"
    ></el-cascader>
  </el-form-item>
</template>

<script>
import {
  treeselectOfSystem,
  treeselectOfSafe,
  treeselectOfDbscan,
  treeselectOfDataHandler,
} from "@/api/system/dept";

export default {
  name: "CommonDept",
  props: {
    currentParent: {
      type: Number,
      default: 0,
    },
  },

  data() {
    return {
      query: {
        deptId: null,
      },
      deptSelect: [],
    };
  },
  mounted() {
    this.$emit("getLoading");
    if (this.currentParent === 1 || this.currentParent === 2) {
      //管理中心1 / 告警中心2
      this.getTreeselectOfSystem();
    } else if (this.currentParent === 3) {
      //接口安全
      this.getTreeselectOfSafe();
    } else if (this.currentParent === 4) {
      //数据监测
      this.getTreeselectOfDbscan();
    } else if (this.currentParent === 5) {
      //策略中心
      this.getTreeselectOfDataHandler();
    }
  },
  methods: {
    // 管理中心/告警中心
    getTreeselectOfSystem() {
      treeselectOfSystem()
        .then((res) => {
          if (res && res.code === 200) {
            this.deptSelect = res.data || [];
            this.setDisabledOption(this.deptSelect);
          } else {
            this.deptSelect = [];
          }
        })
        .catch((err) => {
          this.deptSelect = [];
        })
        .finally(() => {
          this.$emit("getDeptList", this.deptSelect);
        });
    },

    //接口安全
    getTreeselectOfSafe() {
      treeselectOfSafe()
        .then((res) => {
          if (res && res.code === 200) {
            this.deptSelect = res.data || [];
            this.setDisabledOption(this.deptSelect);
          } else {
            this.deptSelect = [];
          }
        })
        .catch((err) => {
          this.deptSelect = [];
        })
        .finally(() => {
          this.$emit("getDeptList", this.deptSelect);
        });
    },

    //数据监测
    getTreeselectOfDbscan() {
      treeselectOfDbscan()
        .then((res) => {
          if (res && res.code === 200) {
            this.deptSelect = res.data || [];
            this.setDisabledOption(this.deptSelect);
          } else {
            this.deptSelect = [];
          }
        })
        .catch((err) => {
          this.deptSelect = [];
        })
        .finally(() => {
          this.$emit("getDeptList", this.deptSelect);
        });
    },

    //策略中心
    getTreeselectOfDataHandler() {
      treeselectOfDataHandler()
        .then((res) => {
          if (res && res.code === 200) {
            this.deptSelect = res.data || [];
            this.setDisabledOption(this.deptSelect);
          } else {
            this.deptSelect = [];
          }
        })
        .catch((err) => {
          this.deptSelect = [];
        })
        .finally(() => {
          this.$emit("getDeptList", this.deptSelect);
        });
    },

    setDisabledOption(data) {
      if (data.length > 0) {
        data.forEach((f) => {
          f.disabled = f.freeze !== "N";
          f.value = f.id;
          if (f.children && f.children.length > 0) {
            this.setDisabledOption(f.children);
          }
        });
      }
    },

    changeValue(val) {
      this.$emit("getDeptId", val.join());
    },
    clearDept() {
      this.query.deptId = null;
      this.$emit("getDeptId", null);
    },
  },
};
</script>

<style scoped lang="scss">
</style>
